using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LayoutPropertyKepper : MonoBehaviour
{
    public void Release()
    {
        var layoutElement = this.gameObject.GetOrAddComponent<LayoutElement>();
        layoutElement.enabled = false;
    }

    public void Record()
    {
        this.SetLayoutPropertyToLayoutElement();
    }

    void SetLayoutPropertyToLayoutElement()
    {
        if(this.transform.transform.childCount == 0)
        {
            return;
        }
        var child = this.transform.transform.GetChild(0).GetComponent<RectTransform>();

        var layoutElement = this.gameObject.GetOrAddComponent<LayoutElement>();
        layoutElement.enabled = false;
        var minWidth = LayoutUtility.GetMinWidth(child);
        var minHeight = LayoutUtility.GetMinHeight(child);
        var prefWidth = LayoutUtility.GetPreferredWidth(child);
        var prefHeight = LayoutUtility.GetPreferredHeight(child);
        var flexWdith = LayoutUtility.GetFlexibleWidth(child);
        var flexHeight = LayoutUtility.GetFlexibleHeight(child);
        layoutElement.minWidth = minWidth;
        layoutElement.minHeight = minHeight;
        layoutElement.preferredWidth = prefWidth;
        layoutElement.preferredHeight = prefHeight;
        layoutElement.flexibleWidth = flexWdith;
        layoutElement.flexibleHeight = flexHeight;
        layoutElement.enabled = true;

    }
}
